﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Utils.DatabaseUtils;
using Utils;

namespace QLCG.DAO
{
    public class tblNguoiDungUI
    {
        public NguoiDung Login(String taiKhoan, String matKhau)
        {
            try
            {
                NguoiDung result = toNguoiDung(SqlHelper.ExecuteDataset(Common.Conn, "tblNguoiDung_Login", taiKhoan, matKhau));
                return result;
            }
            catch (Exception e) { return null; }
        }

        public NguoiDung Get(int idNguoiDung)
        {
            try
            {
                NguoiDung result = toNguoiDung(SqlHelper.ExecuteDataset(Common.Conn, "tblNguoiDung_Get", idNguoiDung));
                return result;
            }
            catch (Exception e) { return null; }
        }


        public int UpdateProfileByAdmin(int idNguoiDung, String hoTen, String email, String phone, String company, byte loaiNguoiDung, bool hoatDong)
        {
            try
            {
                int result = SqlHelper.ExecuteNonQuery(Common.Conn, "tblNguoiDung_ChangeProfileByAdmin", idNguoiDung, hoTen, email, phone, company, loaiNguoiDung, hoatDong);
                return result;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public int UpdateProfile(int idNguoiDung, String hoTen, String email, String phone, String company)
        {
            try
            {
                int result = SqlHelper.ExecuteNonQuery(Common.Conn, "tblNguoiDung_ChangeProfile", idNguoiDung, hoTen, email, phone, company);
                return result;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public int UpdatePass(int idNguoiDung, String oldPass, String newPass)
        {
            try
            {
                int result = Convert.ToInt32(SqlHelper.ExecuteNonQuery(Common.Conn, "tblNguoiDung_ChangePass", idNguoiDung, oldPass, newPass));
                return result;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int Register(string taiKhoan, string matKhau, string hoTen, string email, string phone, string company, byte loaiNguoiDung)
        {
            try
            {
                int result = SqlHelper.ExecuteNonQuery(Common.Conn, "tblNguoiDung_Register", taiKhoan, matKhau, hoTen, email, phone, company, loaiNguoiDung);
                return result;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        private NguoiDung toNguoiDung(DataSet ds)
        {
            try
            {
                if (TableLib.HasData(ds))
                {
                    DataRow row = ds.Tables[0].Rows[0];
                    NguoiDung nd = new NguoiDung();
                    nd.Company = row["Company"].ToString().Trim();
                    nd.Email = row["Email"].ToString().Trim();
                    nd.Phone = row["Phone"].ToString().Trim();
                    nd.HoTen = row["HoTen"].ToString().Trim();
                    nd.TaiKhoan = row["TaiKhoan"].ToString().Trim();
                    nd.IdNguoiDung = Convert.ToInt32(row["IdNguoiDung"]);
                    nd.NgayTao = Convert.ToDateTime(row["NgayTao"]);
                    nd.TaiKhoan = row["TaiKhoan"].ToString().Trim();
                    nd.HoatDong = Convert.ToBoolean(row["HoatDong"]);
                    nd.LoaiNguoiDung = Convert.ToByte(row["LoaiNguoiDung"]);
                    return nd;
                }
                return null;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }

    public class NguoiDung
    {
        private int idNguoiDung;

        public int IdNguoiDung
        {
            get { return idNguoiDung; }
            set { idNguoiDung = value; }
        }
        private String taiKhoan;

        public String TaiKhoan
        {
            get { return taiKhoan; }
            set { taiKhoan = value; }
        }

        private string hoTen;

        public string HoTen
        {
            get { return hoTen; }
            set { hoTen = value; }
        }

        private String matKhau;

        public String MatKhau
        {
            get { return matKhau; }
            set { matKhau = value; }
        }
        private String email;

        public String Email
        {
            get { return email; }
            set { email = value; }
        }
        private String phone;

        public String Phone
        {
            get { return phone; }
            set { phone = value; }
        }
        private String company;

        public String Company
        {
            get { return company; }
            set { company = value; }
        }
        private Byte loaiNguoiDung;

        public Byte LoaiNguoiDung
        {
            get { return loaiNguoiDung; }
            set { loaiNguoiDung = value; }
        }
        private DateTime ngayTao;

        public DateTime NgayTao
        {
            get { return ngayTao; }
            set { ngayTao = value; }
        }
        private Boolean hoatDong;

        public Boolean HoatDong
        {
            get { return hoatDong; }
            set { hoatDong = value; }
        }
    }
}
